//流程定义id
const id = $.getUrlParam('id');

let zTree;

// zTree 配置
var zTreeSetting = {
    async: {
        enable: true,
        url: "/assignee/getSubNodesByDeptId",
        type: "get",
        autoParam: ["id=deptId"],
        otherParam: {"userName": ""}
    }, check: {
        enable: true,
        chkStyle: "radio",
        radioType: "all"
    }
};

//当前点击的办理人
let assignee;

$(function () {

    //弹出提示信息
    $('#alert').modal('toggle');
    setTimeout(function () {
        $('#alert').modal('hide');
    }, 5000);

    //纵向滚动条滚动事件
    $(window).scroll(function () {
        $('.flow-and-task').css('margin-top', $(document).scrollTop());
    });

    //获取流程数据
    $.ajax({
        type: "GET",
        url: "/flowDef/getById",
        contentType: "application/x-www-form-urlencoded",
        dataType: "json",
        data: {"id": id},
        success: function (data) {
            if (data.code === SUCCESS_CODE) {
                //console.log(data);

                let flowDefDto = data.payload;
                let flowDefEntity = flowDefDto.flowDefEntity;

                $('.flow-and-task #flowDefName').text(flowDefEntity.name);
                $('.flow-and-task #flowDefDes').text(flowDefEntity.des);

                let flowDefTaskEntityList = flowDefDto.flowDefTaskEntityList;
                $.each(flowDefTaskEntityList, function () {
                    let flowDefTaskEntity = this;
                    let taskType = flowDefTaskEntity.type;
                    if (taskType === 'start' || taskType === 'end') {
                        return true;//相当于continue，return false 相当于break
                    }

                    let taskNode = $('#taskTemplate [taskType="' + taskType + '"]');
                    let taskNodeClone = taskNode.clone(true);
                    taskNodeClone.find('[name="name"]').text(flowDefTaskEntity.name);
                    taskNodeClone.find('[name="des"]').text(flowDefTaskEntity.des);
                    if (taskType != 'pointTask') {
                        taskNodeClone.find('[name="assignees"]').text(flowDefTaskEntity.assigneesName);
                    } else {
                        taskNodeClone.find('[name="assignees"]').data('flowDefTaskId', flowDefTaskEntity.id);
                    }

                    $('.flow [taskType="end"]').before(taskNodeClone);
                });

            } else {
                toastr.error(data.msg);
            }
        }
    });

    //办理人
    $('.task-property input[name="assignees"]').click(function () {
        assignee = $(this).parent().parent().parent().parent();
        openModal();
    });

    //搜索
    $('.search button').click(function () {
        let search = $(this).parent().find('input').val();
        zTreeSetting.async.otherParam.userName = search;
        getAssignee();
        //阻止默认事件
        return false;
    });

    //选择办理人
    $('#select').click(function () {
        var nodes = zTree.getCheckedNodes(true);
        var userIdArray = [];
        var userNameArray = [];
        $.each(nodes, function () {
            let node = this;
            if (node.isUser) {
                userIdArray.push(node.id);
                userNameArray.push(node.name);
            }
        });
        assignee.find('[name="assignees"]').data('userIdArray', userIdArray);
        assignee.find('[name="assignees"]').val(userNameArray.join(','));
        $('#assignee').modal('toggle');
    });

    //保存工作流
    $('.flow .save').click(function () {

        let assigneesId = new Object();
        let checkPass = true;

        $('.flow .task[taskType="pointTask"]').each(function () {
            let assigneesIds = $(this).find('[name="assignees"]').data('userIdArray');
            let flowDefTaskId = $(this).find('[name="assignees"]').data('flowDefTaskId');

            if ($.isEmptyObject(assigneesIds)) {
                toastr.warning('请为 “指定型” 任务选择任务办理人。');
                checkPass = false;
                return false;
            }

            assigneesId[flowDefTaskId] = assigneesIds.join(',');
        });

        if (checkPass) {
            //console.log(assigneesId);
            //携带任务办理人参数，跳转到填写流程表单页面
            goto("/admin/flowinst/start_flow.html?flowDefId="+ id +"&assignees=" + encodeURIComponent(JSON.stringify(assigneesId)));
        }
    });

});

/**
 * 打开模态框，选择办理人
 */
function openModal() {
    $('#assignee').modal('toggle');
    getAssignee();
}

function getAssignee() {
    zTree = $.fn.zTree.init($("#assigneeTree"), zTreeSetting);
}

